﻿using MyBlog.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.Repository
{
    public class UserBlogRepository : IRepository<UserBlog>
    {
        private BlogDBContext _dbContext;
        public UserBlogRepository(BlogDBContext dbContext)
        {
            _dbContext = dbContext;
        }
        public bool Add(UserBlog userBlog)
        {
            _dbContext.UserBlog.Add(userBlog);
            int flag=_dbContext.SaveChanges();
            return flag>0?true:false;
        }

        public bool Delete(int id)
        {
            var userBlog = _dbContext.UserBlog.Find(id);
            if (userBlog != null)
                _dbContext.UserBlog.Remove(userBlog);
            int flag = _dbContext.SaveChanges();
            return flag > 0 ? true : false;
        }

        public UserBlog Get(int id)
        {
            return _dbContext.UserBlog.Find(id); 
        }

        public List<UserBlog> GetAll()
        {
            return _dbContext.UserBlog.ToList();
        }

        public bool Update(UserBlog userBlog)
        {
            var muserBlog = _dbContext.UserBlog.Attach(userBlog); ;
            muserBlog.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            int flag = _dbContext.SaveChanges();
            return flag > 0 ? true : false;
        }
    }
}
